from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


# 定义一个类
class BasePage:
    # ================================================================
    # 创建一个init绑定driver
    def __init__(self, d):
        self.driver = d

    # 封装显示等待(单个元素的)
    def by_findelement(self, *item):
        try:
            # 等待某个元素出现,最长等待10秒,每0.5秒检查一次,渲染完成则继续下一步操作
            element = WebDriverWait(self.driver, 10).until(EC.presence_of_element_located(item))
        except Exception as msg:
            print(f"页面元素:{item}定位失败,提示:{msg}")
            raise
        else:
            return element

    # 封装显示等待(多个元素的)
    def by_findelements(self, *item):
        try:
            # 等待某个元素出现,最长等待10秒,每0.5秒检查一次,渲染完成则继续下一步操作
            element = WebDriverWait(self.driver, 10).until(EC.presence_of_all_elements_located(item))
        except Exception as msg:
            print(f"页面元素:{item}定位失败,提示:{msg}")
            raise
        else:
            return element